# Builder.py: builder state
# Written by Victor Luna Freire - victorclf@live.com
# Copyright (c) Victor Luna Freire. All rights reserved.
# Monday, November 02, 2009 2:23:00 AM
# Python 2.6.2

import pygame
import os

import Timer
import Input
import Defines
import Maze
import SpecialText
import Maps
import Gamescript

                

class Builder:
    def __init__(self):
        #self.text_building = self.afont_big.render('Construindo', True, (255, 255, 255), (49, 106, 197))
        #self.text_building =
        #self.text_building_speed = 20
        #self.text_building_current = 170
        #self.afont_big.set_bold(True)
        self.bfont_big = pygame.font.Font('MAZE.TTF', 36)
        self.text_building = SpecialText.GlowingText(self.bfont_big, "Construindo", (226, 435), 2.8, (130, 0, 0), (255, 0, 0), (0, 0, 0))
        self.text_built = SpecialText.GlowingText(self.bfont_big, "Espaco para prosseguir", (140, 435), 4.3, (130, 0, 0), (255, 0, 0), (0, 0, 0))
        self.build_cell_delay = 1000.0 / Defines.BUILD_CELL_SPEED
        self.build_time_counter = 0
                
    
            
    def new_builder(self):
        #Maps.Maps.maps = {}
        #Maps.Maps.objects = {}
        pygame.mixer.music.set_volume(0.5)
        self.state.append(self.builder)
        Maze.Maze.new_maze(Defines.DEFAULT_MAZE_WIDTH, Defines.DEFAULT_MAZE_HEIGHT)
        Gamescript.SoundDirector.sound_event('building')
    
    
            
    def builder_input(self, mult_factor):
        keep_running = Input.Input.get_input()
                
        if Input.Input.check_once(pygame.K_ESCAPE):
            keep_running = False
            
        if Input.Input.check_once(pygame.K_SPACE):
            if not Maze.Maze.builder_is_done():
                Maze.Maze.builder_finish()
                Gamescript.SoundDirector.sound_event('built')
            else:
                self.new_game()
            
        #if Input.Input.check(pygame.K_p):
        #    self.new_game()
        
        return keep_running
    
    
    
    def builder(self):
        if not self.builder_input(self.mult_factor): return False
        
        self.screen.fill((0, 0, 0))
        
        self.screen.blit(Maze.Maze.get_grid_surface(), (0,0))
        
        # Build counter manger
        self.build_time_counter += Timer.Timer.get_time_variation()
        if not Maze.Maze.builder_is_done() and self.build_time_counter > self.build_cell_delay:
            for i in xrange(int(self.build_time_counter % self.build_cell_delay)):
                if Maze.Maze.builder_next_step():
                    if Maze.Maze.builder_is_done():
                        Gamescript.SoundDirector.sound_event('built')
                        break
            self.build_time_counter = 0
        
        if not Maze.Maze.builder_is_done(): self.text_building.main(self.screen, self.mult_factor)
        else: self.text_built.main(self.screen, self.mult_factor)
                               
        pygame.display.flip()
        return True
    



